<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="付款类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.money }}
          <div v-if="scope.row.payType=='wx'">微信</div>
          <div v-if="scope.row.payType=='zfb'">支付宝</div>
        </template>
      </el-table-column>
      <el-table-column label="收款码" align="center">
        <template slot-scope="scope">
          <img style="width:100px;height:100px" :src="scope.row.payCode" alt="">
        </template>
      </el-table-column>
           <el-table-column label="收款状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.money }}
          <div v-if="scope.row.status==1" style="color:green">开启</div>
          <div v-if="scope.row.status==0" style="color:red">关闭</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button  type="text" style="color:blue" @click="editOrAdd(1,scope.row)">编辑</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
     <el-dialog :title="dialogTitles[dialogIndex]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="adminData" label-width="100px" style="margin-left:100px;margin-right: 100px">
        <el-form-item label="收款码" prop="avart">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="adminData.payCode" :src="adminData.payCode" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="收款类型" prop="payType">
            <el-select v-model="adminData.payType" class="filter-item" style="width:100%" placeholder="请选择导入来源">
              <el-option key="wx" label="微信" value="wx" />
              <el-option key="zfb" label="支付宝" value="zfb" />
          </el-select>
        </el-form-item>
          <el-form-item label="收款状态" prop="status">
            <el-select v-model="adminData.status" class="filter-item" style="width:100%" placeholder="请选择预付款金额状态">
              <el-option key="0" label="关闭" :value="0" />
              <el-option key="1" label="开启" :value="1" />
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { payList,paySet} from '@/api/user'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import Calendar from '@/components/Cascader'
export default {
  components: { Pagination, Calendar },
  directives: { waves },
  data() {
    return {
      action:process.env.VUE_APP_BASE_API+"/sysUser/upImgs.json",
      list: [],
      listLoading: true,
      total: 10,
      dialogTitles: ['添加', '编辑'],
      dialogIndex: 0,
      dialogFormVisible: false,
      adminData: {},
      params: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
         payCode: [{ required: true, message: '请上传收款码', trigger: 'blur' }],
          payType: [{ required: true, message: '请选择收款类型', trigger: 'blur' }],
          status: [{ required: true, message: '请选择是否开启', trigger: 'blur' }],

      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
        // 图片上传
      handleAvatarSuccess(res, file) {
        this.adminData.payCode='http://39.105.201.40:8081'+res.src;
      },
    fetchData() {
      this.listLoading = true
      payList(this.params).then(response => {
        this.list=response
        this.listLoading = false
      })
    },
    editOrAdd(index, item) {
      this.adminData = Object.assign({}, item)
      if (index === 1) {
        delete this.adminData.createTime
        delete this.adminData.updateTime
      }
      this.dialogIndex = index
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
      save() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          paySet(this.adminData).then(res => {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: this.dialogTitles[this.dialogIndex] + '成功!'
            })
            this.fetchData()
          })
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
    /deep/.el-dialog{
            width:40%
    }
        /deep/.el-upload{
      border: 1px solid #d9d9d9;
      border-radius: 5px;
      margin: 0 auto;
    }
     .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
